<template>
  <div>
    <danger-detail-info :form="form"></danger-detail-info>
    <div class="block-box">
      <div class="block-box_item">
        <div class="block-box_title">指派整改人</div>
        <div>
          <div class="det_tab_title"></div>
          <van-field
            class="input_text"
            name="radio"
            label="判定"
            label-width="80"
          >
            <template #input>
              <van-radio-group v-model="radio" direction="horizontal">
                <van-radio icon-size="18px" name="1">指派</van-radio>
                <van-radio icon-size="18px" name="2">退回隐患</van-radio>
              </van-radio-group>
            </template>
          </van-field>
          <div v-if="radio == 2">
            <van-field
              v-model="remarks"
              rows="3"
              autosize
              required
              label-width="80"
              label="退回说明"
              type="textarea"
              placeholder="请输入"
              show-word-limit
            />
          </div>
          <div v-else>
            <van-field
              class="input_text"
              label-width="80"
              :value="getCorrectUserText(editForm)"
              label="整改人"
              placeholder="请选择"
              required
              readonly
              @click="openDeprtmentUserChooseWindow()"
            >
              <template #right-icon v-if="radio != 2"
                ><van-icon name="arrow"
              /></template>
            </van-field>
            <van-field
              v-if="!isMe"
              v-model="editForm.Remarks"
              label-width="80"
              rows="3"
              autosize
              label="说明"
              type="textarea"
              placeholder="请输入"
            />

            <div v-if="isMe">
              <van-field
                class="input_text"
                label-width="80"
                required
                name="radio"
                label="整改方式"
              >
                <template #input>
                  <van-radio-group
                    v-model="editForm.CorrectiveMethod"
                    direction="horizontal"
                  >
                    <van-radio icon-size="18px" name="1">立即整改</van-radio>
                    <van-radio icon-size="18px" name="2">限期整改</van-radio>
                  </van-radio-group>
                </template>
              </van-field>
              <van-field
                label-width="80"
                v-if="editForm.CorrectiveMethod == '1'"
                required
                class="input_text"
                name="uploader"
                label="整改图片"
              >
                <template v-if="editForm.CorrectiveMethod == '1'" #input>
                  <eagle-image-uploader
                    :images.sync="editForm.CorrectiveAttachs"
                  ></eagle-image-uploader>
                </template>
              </van-field>
              <van-field
                label-width="80"
                v-if="editForm.CorrectiveMethod == '1'"
                required
                v-model="editForm.CorrectiveDesc"
                rows="3"
                autosize
                label="整改说明"
                type="textarea"
                placeholder="请输入"
              />
              <date-picker
                label="整改日期"
                labelWidth="80"
                v-if="editForm.CorrectiveMethod == '2'"
                required
                :date.sync="editForm.CorrectiveDate"
              ></date-picker>
              <van-field
                v-if="editForm.CorrectiveMethod == '2'"
                required
                v-model="editForm.CorrectiveMeasure"
                rows="2"
                autosize
                label="整改措施"
                label-width="80"
                type="textarea"
                placeholder="请输入"
                show-word-limit
              />
            </div>
          </div>
        </div>
      </div>
      <div class="btns mt-10 mb-10">
        <van-button class="btn" @click="submit"> 提交 </van-button>
      </div>
      <user-choose-dialog
        :show.sync="deprtmentUserChooseWindowShow"
        @callback="departmentUserChooseCallback"
      />
    </div>
  </div>
</template>

<script>
import { getDangerDetail } from "@/api/sytech/sytechDangerCheckTaskDetail";
import {
  confirmAssign,
  backAssign,
} from "@/api/sytech/sytechDangerCheckTaskDetail";
import { getDate, dateAdd } from "@/utils/getDate";
import { deepClone } from "@/utils";
import { mapGetters } from "vuex";
import DangerDetailInfo from "@/views/sytech/sytechDangerCheckTaskDetail/DangerDetailInfo.vue";
import UserChooseDialog from "@/views/sytech/sytechDangerCheckTaskDetail/UserChooseDialog.vue";
export default {
  components: {
    UserChooseDialog,
    DangerDetailInfo,
  },
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      id: this.$route.query.id,
      border: false,
      active: "a",
      reason: false,
      expenses: false,
      showCalendar: false,
      correctiveDate: null,
      isMe: false,
      form: {},
      editForm: {},
      radio: "1",
      name: "",
      remarks: "",
      deprtmentUserChooseWindowShow: false,
    };
  },
  computed: {
    ...mapGetters(["token"]),
  },
  created() {
    this.geDetail();
  },
  methods: {
    // 获取详情
    geDetail() {
      getDangerDetail({ id: this.$route.query.id }).then((res) => {
        this.$set(this, "form", res.data);
        this.editForm = deepClone(res.data);
        this.editForm.CorrectiveDeptName = "";
        this.editForm.CorrectiveDeptCode = "";
        this.editForm.CorrectiveDate = getDate(
          this.editForm.CorrectiveDate || dateAdd("w", 1, new Date())
        );
        this.editForm.CorrectiveMethod = this.editForm.CorrectiveMethod || "2";
        this.correctiveDate = new Date(this.editForm.CorrectiveDate);
      });
    },
    openDeprtmentUserChooseWindow() {
      this.deprtmentUserChooseWindowShow = true;
    },
    // 选择部门人回调
    departmentUserChooseCallback(item, type) {
      if (item) {
        if (type === 0) {
          this.name = item.OrgName;
        } else {
          this.name = item.ChnName;
        }
        this.editForm.CorrectiveDeptName = item.OrgName;
        this.editForm.CorrectiveDeptCode = item.OrgCode;
        this.editForm.CorrectiveChnName = item.ChnName;
        this.editForm.CorrectiveUserName = item.UserName;
        this.isMe = this.token == this.editForm.CorrectiveUserName;
      }
      this.deprtmentUserChooseWindowShow = false;
    },
    getCorrectUserText(item) {
      if (item.CorrectiveDeptName || item.CorrectiveChnName) {
        if (item.CorrectiveChnName && item.CorrectiveDeptName) {
          return item.CorrectiveDeptName + "-" + item.CorrectiveChnName;
        } else if (item.CorrectiveChnName) {
          return item.CorrectiveChnName;
        } else {
          return item.CorrectiveDeptName;
        }
      }
      return "";
    },
    openCalendar() {
      this.showCalendar = true;
    },
    onCalendarCallback(date) {
      this.editForm.CorrectiveDate = getDate(date);
      this.correctiveDate = date;
      this.showCalendar = false;
    },
    submit() {
      // this.loading = true
      if (this.radio == 1) {
        let param = {
          ID: this.editForm.ID,
          CorrectiveDeptName: this.editForm.CorrectiveDeptName,
          CorrectiveDeptCode: this.editForm.CorrectiveDeptCode,
          CorrectiveChnName: this.editForm.CorrectiveChnName,
          CorrectiveUserName: this.editForm.CorrectiveUserName,
          CorrectiveMethod: this.editForm.CorrectiveMethod,
          CorrectiveDate: this.editForm.CorrectiveDate,
          CorrectiveMeasure: this.editForm.CorrectiveMeasure,
          CorrectiveDesc: this.editForm.CorrectiveDesc,
          Analysis: this.editForm.Analysis,
          Expenses: this.editForm.Expenses,
          Remarks: this.editForm.Remarks,
          CorrectiveAttachs: this.editForm.CorrectiveAttachs,
        };
        if (!param.CorrectiveDeptCode && !param.CorrectiveUserName) {
          this.$toast("请选择整改人");
          return false;
        }
        if (param.CorrectiveUserName == this.token) {
          if (param.CorrectiveMethod == "1") {
            if (!param.CorrectiveAttachs) {
              this.$toast("请选择整改图片");
              return false;
            }
            if (!param.CorrectiveDesc) {
              this.$toast("请输入整改说明");
              return false;
            }
          } else {
            if (!param.CorrectiveDate) {
              this.$toast("请选择整改日期");
              return false;
            }
            if (!param.CorrectiveMeasure) {
              this.$toast("请输入整改措施");
              return false;
            }
          }
        }
        confirmAssign(param)
          .then((res) => {
            this.$toast("提交成功");
            this.$router.goRefresh();
          })
          .catch((err) => {
            console.log("err", err);
          });
      } else {
        let param = {
          ID: this.editForm.ID,
          Remarks: this.remarks,
        };
        if (!param.Remarks) {
          this.$toast("请输入退回说明");
          return false;
        }
        backAssign(param)
          .then((res) => {
            this.$toast("提交成功");
            this.$router.goRefresh();
          })
          .catch((err) => {
            console.log("err", err);
          });
      }
    },
    // 一周
    getOneWeek() {
      let date = getDate(dateAdd("w", 1, new Date()));
      this.$set(this.editForm, "CorrectiveDate", date);
      this.correctiveDate = new Date(date);
    },
    // 两周
    getTwoWeek() {
      let date = getDate(dateAdd("w", 2, new Date()));
      this.$set(this.editForm, "CorrectiveDate", date);
      this.correctiveDate = new Date(date);
    },
    getOneMounth() {
      let date = getDate(dateAdd("m", 1, new Date()));
      this.$set(this.editForm, "CorrectiveDate", date);
      this.correctiveDate = new Date(date);
    },
  },
};
</script>
